package com.edu.dao;

import java.sql.*;

/**
 * @Auther:zzp
 * @Date:2025/9/19-14:27:21
 */
public class BaseDao {
    private static final String DRIVER = "com.mysql.jdbc.Driver";

    private static final String URL = "jdbc:mysql://localhost:3306/shop";

    private static final String USER = "root";

    private static final String PASSWORD = "123456";


    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("加载驱动失败");
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            System.out.println("请检查数据库连接，用户名和密码");
            e.printStackTrace();
        }
        return con;
    }

    public static void closeAll(ResultSet res, Statement stmt, Connection con) {
        if(res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
